package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.kitchen.KitchenFoodType;
import org.dromara.kitchen.domain.kitchen.KitchenTopicType;
import org.dromara.kitchen.service.IKitchenTopicTypeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 美食分类类型
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/topicType")
public class KitchenTopicTypeController extends BaseController {

    private final IKitchenTopicTypeService kitchenTopicTypeService;

    /**
     * 查询美食分类类型列表
     */
    @SaCheckPermission("kitchen:topicType:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenTopicType> list(KitchenTopicType kitchenTopicType, PageQuery pageQuery) {
        QueryWrapper<KitchenTopicType> qw = new QueryWrapper<>();

        TableDataInfo<KitchenTopicType> td = PageUtils.buildDataInfo(kitchenTopicTypeService.page(PageUtils.buildPage(),qw));
        List<KitchenTopicType> list = td.getRows();
        return td;
    }


    /**
     * 查询套餐主题列表  不分页
     */
    @GetMapping("/noPage/list")
    public R<List<KitchenTopicType>> noPageList(KitchenTopicType topicType) {
        QueryWrapper<KitchenTopicType> qw = new QueryWrapper<>();
        List<KitchenTopicType> list = kitchenTopicTypeService.list(qw);
        return R.ok(list);
    }



    /**
     * 导出美食分类类型列表
     */
    @SaCheckPermission("kitchen:topicType:export")
    @Log(title = "美食分类类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenTopicType kitchenTopicType, HttpServletResponse response) {
        List<KitchenTopicType> list = kitchenTopicTypeService.list();
        ExcelUtil.exportExcel(list, "美食分类类型", KitchenTopicType.class, response);
    }

    /**
     * 获取美食分类类型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:topicType:query")
    @GetMapping("/{id}")
    public R<KitchenTopicType> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(kitchenTopicTypeService.getById(id));
    }

    /**
     * 新增美食分类类型
     */
    @SaCheckPermission("kitchen:topicType:add")
    @Log(title = "美食分类类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenTopicType kitchenTopicType) {
        return toAjax(kitchenTopicTypeService.save(kitchenTopicType));
    }

    /**
     * 修改美食分类类型
     */
    @SaCheckPermission("kitchen:topicType:edit")
    @Log(title = "美食分类类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenTopicType kitchenTopicType) {
        return toAjax(kitchenTopicTypeService.updateById(kitchenTopicType));
    }

    /**
     * 删除美食分类类型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:topicType:remove")
    @Log(title = "美食分类类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenTopicTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
